/**
 * @description  : 通知公告-待办处理页面
 * @descriptionDetail : 通知公告-待办处理页面
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-12-05 15:48:15
 */
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif -->
    <view class="content">
      <u-form :model="orgForm" label-position="top" label-width="auto" ref="uForm">
        <u-form-item label="发布类型:">
          {{ orgForm.noticeTypeText || "/" }}
        </u-form-item>
        <u-form-item label="发布单位:">
          {{ orgForm.publishOrgName || "/" }}
        </u-form-item>
        <u-form-item label="发布部门:">
          {{ orgForm.publishDeptName || "/" }}
        </u-form-item>
        <u-form-item label="发布人:">
          {{ orgForm.publishPerson || "/" }}
        </u-form-item>
        <u-form-item label="缓急程度:">
          {{ orgForm.urgentDegreeText || "/" }}
        </u-form-item>
        <u-form-item label="标题:">
          {{ orgForm.title || "/" }}
        </u-form-item>
        <u-form-item label="发布范围:" v-if="noticeTypeO == noticeType[0].bcbCode">
          {{ $u.dic.getDicNameByKeyCode($u.dic.DIC_KEYS.T_NOTICE_PUBLISH_RANGE,orgForm.publishArea) || "/" }}
        </u-form-item>
        <!-- 通知的 -->
        <u-form-item label="接收人员:" v-if="noticeTypeO == noticeType[0].bcbCode && publishRange == publishRange[2].bcbCode">
          {{ orgForm.receiveObjectName || "/" }}
        </u-form-item>
        <u-form-item label="接收单位/部门:" v-else>
          {{ orgForm.receiveObjectName || "/" }}
        </u-form-item>
        <!-- 公告的 -->
        <u-form-item label="是否需要反馈:" v-if="noticeTypeO == noticeType[0].bcbCode">
          {{ orgForm.isFeedbackText || "/" }}
        </u-form-item>
        <u-form-item label="反馈截止时间:" v-if="noticeTypeO == noticeType[0].bcbCode">
          {{ orgForm.feedbackStopDate || "/" }}
        </u-form-item>
        <u-form-item label="是否发短信提醒:">
          {{ orgForm.isSendText || "/" }}
        </u-form-item>
        <u-form-item label="发布日期:">
          {{ orgForm.publishTime || "/" }}
        </u-form-item>
        <u-form-item label="正文:">
          <view v-html="orgForm.content" v-if="orgForm.content" class="detail-content-sty">
          </view>
          <view v-else>
            {{ "/" }}
          </view>
        </u-form-item>
        <u-form-item label="相关附件">
          <attachment-upload :noticeUuid="orgForm.noticeUuid"></attachment-upload>
        </u-form-item>
        <u-form-item label="审批意见" v-if="step === 'onlySubmit'">
          <u-input :maxlength="255" placeholder="请输入审批意见" type="textarea" v-model="orgForm.approvalIdea" />
        </u-form-item>
        <u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()" :height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_form_item_bottom_border_color"></u-gap>
        <fsection title="审批历史"></fsection>
        <u-form-item label="" :border-bottom="false">
          <view style="width: 100%;color: #000000;">
            <flow-his-no-doc :biz-uuid="orgForm.noticeUuid" process-def-key="NoticeInfoPublish" :endTitle="step == 'view' ? '结束':'...'"></flow-his-no-doc>
          </view>
        </u-form-item>
      </u-form>
    </view>
    <view class="submit-btns btn-other-cls" v-if="step === 'onlySubmit'">
      <u-button class="u-btn" type="error" @click="handlerApproval(1)">
        退回
      </u-button>
      <u-button class="u-btn" type="primary" @click="handlerApproval(0)">
        审批通过
      </u-button>
    </view>
    <u-popup v-model="personChoseVisible" length="80%" mode="bottom">
      <flow-user-select-com 
        :flow-nodes="approve"
        :biz-uuid="orgForm.noticeUuid"
        flow-code="NoticeInfoPublish"
        @flowUserSelectBack="onPersonChose"
      ></flow-user-select-com>
    </u-popup>
  </view>
</template>

<script>
import fsection from "@/components/form-section.vue";
import teamNoticeService from "@/service/team/notice.service";
import attachmentUpload from "@/pages/notice/attachmentUpload.vue";
import FlowUserSelectCom from "@/pages/commonPage/flowUserSelectCom.vue";
import FlowHisNoDoc from "@/components/flow-his-no-doc.vue";
export default {
  components: {
    attachmentUpload,
    FlowUserSelectCom,
    FlowHisNoDoc,
    fsection,
  },
  data() {
    return {
      lab_style:{
        fontWeight: 500,
        color: this.$theme.u_main_color,
        fontSize: '.75rem',
      },
      iconStyle:{
        color: '#c0c4cc',
      },
      orgForm:{
        noticeUuid:"",
        approvalIdea:"",
      },
      flowHis: [],
      noticeType: this.$u.dic.getDicByKeyCode(this.$u.dic.DIC_KEYS.T_NOTICE_PUBLISH_TYPE),
      publishRange: this.$u.dic.getDicByKeyCode(this.$u.dic.DIC_KEYS.T_NOTICE_PUBLISH_RANGE),
      noticeTypeO:'',
      T_NOTICE_PUBLISH_STATUS: this.$u.dic.getDicByKeyCode(this.$u.dic.DIC_KEYS.T_NOTICE_PUBLISH_STATUS),  // 发布状态
      approve:[],
      nextPersonInfo:{},
      personChoseVisible:false,
      step:"onlySubmit", // 流程状态  默认审批
    }
  },
  onLoad(opt) {
    let p = this.getRouteParam() || {};
    this.init(p.bizUuid);
    this.orgForm.noticeUuid = p.bizUuid;
  },
  methods: {
    init(id){
      this.$modal.showLoading('加载中...');
      teamNoticeService.findById({id:id,}).then(res => {
        if (res.success) {
          this.$modal.hideLoading();
          this.orgForm = res.bean || {};
          this.step = this.orgForm.step;
          this.noticeTypeO = res.bean.noticeType;
          if(res.bean.step == "edit") return this.$u.toast("已退回，请在pc端重新提交！")
        }else{
          this.$modal.hideLoading();
          this.$u.toast(res.msg)
        }
      }).catch(err => {
        this.$modal.hideLoading();
        this.$u.toast('加载失败')
        console.log(err);
      })
    },
    onPersonChose(nextInfo){
      this.nextPersonInfo = {
        nextPersonNames:nextInfo.map(itm => {
          return itm.personName;
        }).join(','),
        nextPersonUuids:nextInfo.map(itm => {
          return itm.personId;
        }).join(','),
      };
      this.personChoseVisible = false;
      this.handlerApproval(0);
    },
    getGapMarginRowNumber(){
      return "-" + this.$theme.cm_content_paddng_row.replace('rpx', '');
    },
    handlerApproval(type){
      if(this.$u.test.isEmpty(this.orgForm.approvalIdea)) return this.$u.toast('请填写审批意见')
      if(type == 0){
        this.$modal.showLoading('加载中...');
        teamNoticeService.getFlowIsFinish({bizUuid:this.orgForm.noticeUuid,}).then(res => {
          if (res.success) {
            if(res.bean){
              // 如果是流程最后节点
              this.$modal.hideLoading();
              const params = {
                noticeUuid:this.orgForm.noticeUuid,
                approvalIdea:this.orgForm.approvalIdea,// 审批意见
                status:this.T_NOTICE_PUBLISH_STATUS[1].bcbCode,// 已发布状态
                nextPersonUuids: '', // 审批人
              };
              teamNoticeService.approval(params).then(res => {
                if (res.success) {
                  this.$u.toast('审批通过')
                  this.backPrev({
                    paramKey:'refresh',
                    param:true,
                  })
                }else {
                  this.$u.toast(res.msg)
                }
              }).catch(err => {
                this.$u.toast('审批失败')
                console.log(err);
              })
            }else{
              // 不是流程的最后节点
              if (!this.nextPersonInfo.nextPersonUuids) {
                this.personChoseVisible = true;
                return false;
              }
              this.$modal.hideLoading();
              const params = {
                noticeUuid:this.orgForm.noticeUuid,
                approvalIdea:this.orgForm.approvalIdea,// 审批意见
                status:this.T_NOTICE_PUBLISH_STATUS[1].bcbCode,// 已发布状态
                nextPersonUuids:this.nextPersonInfo.nextPersonUuids, // 审批人
              };
              teamNoticeService.approval(params).then(res => {
                if (res.success) {
                  this.$u.toast('审批通过')
                  this.backPrev({
                    paramKey:'refresh',
                    param:true,
                  })
                }else {
                  this.$u.toast(res.msg)
                }
              }).catch(err => {
                this.$u.toast('审批失败')
                console.log(err);
              })
            }
          }else{
            this.$modal.hideLoading();
            this.$u.toast(res.msg)
          }
        }).catch(err => {
          this.$modal.hideLoading();
          this.$u.toast(err)
          console.log(err);
        })
      }else{
        this.$modal.confirm("确定退回吗?", () => {
          this.$modal.showLoading('退回中...');
          const params = {
            noticeUuid:this.orgForm.noticeUuid,
            approvalIdea:this.orgForm.approvalIdea,// 审批意见
            status:this.T_NOTICE_PUBLISH_STATUS[0].bcbCode,// 已发布状态
          };
          teamNoticeService.approval(params).then(res => {
            if (res.success) {
              this.$modal.hideLoading();
              this.backPrev({
                paramKey:'refresh',
                param:true,
              })
            }
          }).catch(err => {
            this.$modal.hideLoading();
            this.$u.toast('退回失败')
            console.log(err);
          })
        });
      }
    },
  },
}
</script>

<style scoped lang="scss">
	.page{
		padding: 0;
	}
.btn{
  position: fixed;
	bottom: 50rpx;
	margin: auto;
	left: 60rpx;
	right: 60rpx;
  z-index: 100;
  background-color: #ffffff;
}
.content{
	padding: $cm-conent-padding-column $cm-content-paddng-row;
}
</style>
